package restaurant.agents;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import java.util.ArrayList;
import java.util.Hashtable;

import restaurant.behaviours.master.MasterBillBehaviour;
import restaurant.behaviours.master.MasterBillChoiceBehaviour;
import restaurant.behaviours.master.MasterCallBehaviour;
import restaurant.behaviours.master.MasterCallChoiceBehaviour;
import restaurant.behaviours.master.MasterReadyBehaviour;
import restaurant.behaviours.master.MasterSyncroBehaviour;
import restaurant.core.Restaurant;

public class MasterWaiterAgent extends Agent {

	private static final long serialVersionUID = -2920932530805722468L;
	public Restaurant restaurant;
	public Hashtable<AID, Boolean> freeWaiters;
	public ArrayList<Long> calledTime;

	private float score = 0;
	
	@Override
	protected void setup() {
		this.restaurant = Restaurant.getInstance();
		this.freeWaiters = new Hashtable<AID, Boolean>();
		this.calledTime = new ArrayList<Long>();

		DFAgentDescription agentDescription = new DFAgentDescription();
		agentDescription.setName(getAID());
		
		ServiceDescription serviceDescription = new ServiceDescription();
		serviceDescription.setType("waiters-management");
		serviceDescription.setName("JADE-restaurant");
		
		agentDescription.addServices(serviceDescription);
		try {
			DFService.register(this, agentDescription);
		} catch (FIPAException e) {
			e.printStackTrace();
		}

		addBehaviour(new MasterSyncroBehaviour(this));
		addBehaviour(new MasterCallBehaviour(this));
		addBehaviour(new MasterCallChoiceBehaviour(this));
		addBehaviour(new MasterBillBehaviour(this));
		addBehaviour(new MasterBillChoiceBehaviour(this));
		addBehaviour(new MasterReadyBehaviour(this));
	}
	
	@Override
	protected void takeDown() {
		try {
			DFService.deregister(this);
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		System.out.println("MasterWaiterAgent: Agente " + getLocalName() + " terminato!");
	}
	
	public void setScore(float score) {
		this.score = score;
		
		this.restaurant.masterWaiterAgentScore = this.score;
	}
	
	public float getScore() {
		return score;
	}
}